<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Config;
use think\Db;
use think\Exception;
use fast\Payment;
/**
 * 个人中心
 */
class Member extends Frontend
{
    protected $layout = '';
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    //会员购买
    public function level(){
        if($this->request->isAjax()){
            $data = $this->request->param();
            $level = Db::name('UserLevel')->where('id',$data['level'])->find();
            if($data['pay'] == 'wechat'){
                $result = Payment::wechat($level['id'],'level',$level['price'],$this->auth->id);
            }
            if($data['pay'] == 'alipay'){
                $result = Payment::alipay($level['id'],'level',$level['price'],$this->auth->id);
            }
            if(!$result){
                $this->error('创建支付订单失败');
            }else{
                $this->success('','',$result);
            }
        }
        return $this->view->fetch();
    }
    //身份审核
    public function audit($id){
        if($this->request->isAjax()){
            
        }
       $group = model('UserGroup')->where('id',$id)->find();
       $user = model('User')->where('id',$this->auth->id)->find();
       $user['area'] = $user['district']?$user['province'].'/'.$user['city'].'/'.$user['district']:"";
       $this->assign('row',$user);
       $this->assign('group',$group);
       return $this->view->fetch();
    }
    //会员提交
    public function submit(){
        $type = input('t',''); //
        $ctl = input('c',''); //favorites fa | comments flag | Follow fo | at 关注 |member/submit?c=bm&t=sp 活动报名 | tel
        $id = input('id');
        if($this->request->isAjax()){
            Db::startTrans();
            try {
                $message = '';
                switch ($ctl) {
                    case 'adv':
                        Db::name('AdviserLog')->where('id',$id)->setInc('likes',1);
                        $message = '点赞成功';
                        break;
                    case 'at':
                        $attention = Db::name('UserAttention')->where('user_id',$id)->where('attention_id',$this->auth->id)->find();
                        if(!empty($attention)){
                            throw new Exception('您已关注过该用户');
                        }
                        Db::name('UserAttention')->insert(['user_id'=>$id,'attention_id'=>$this->auth->id]);
                        $message = '关注成功';
                        break;
                    case 'fa':
                        switch ($type) {
                            case 'document':
                                $favorites = Db::name('DocumentUserFavorites')->where('user_id',$this->auth->id)->where('document_id',$id)->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已收藏过该帖子');
                                }
                                $doc = Db::name('Document')->where('id',$id)->find();
                                model('DocumentUserFavorites')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                switch ($doc['type']) {
                                    case 'wd':
                                        Db::name('Wdocument')->where('id',$doc['doc_id'])->setInc('favorites',1);
                                        Db::name('Document')->where('id',$id)->setInc('favorites',1);
                                        $message = '收藏成功';
                                        break;
                                    case 'wf':
                                        Db::name('Wfdocument')->where('id',$doc['doc_id'])->setInc('favorites',1);
                                        Db::name('Document')->where('id',$id)->setInc('favorites',1);
                                        $message = '收藏成功';
                                        break;
                                    case 'wp':
                                        Db::name('Wpdocument')->where('id',$doc['doc_id'])->setInc('favorites',1);
                                        Db::name('Document')->where('id',$id)->setInc('favorites',1);
                                        $message = '收藏成功';
                                        break;
                                    case 'wr':
                                        Db::name('Resource')->where('id',$doc['doc_id'])->setInc('favorites',1);
                                        Db::name('Document')->where('id',$id)->setInc('favorites',1);
                                        $message = '收藏成功';
                                        break;
                                    case 'week':
                                        Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('favorites',1);
                                        Db::name('Document')->where('id',$id)->setInc('favorites',1);
                                        $message = '收藏成功';# code...
                                        break;
                                    case 'day':
                                        Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('favorites',1);
                                        Db::name('Document')->where('id',$id)->setInc('favorites',1);
                                        $message = '收藏成功';# code...
                                        break;
                                    case 'fa':
                                        Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('favorites',1);
                                        Db::name('Document')->where('id',$id)->setInc('favorites',1);
                                        $message = '收藏成功';# code...
                                        break;
                                }
                                break;
                            case 'wd':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wd')->find();
                                $favorites = Db::name('DocumentUserFavorites')->where('user_id',$this->auth->id)->where('document_id',$doc['id'])->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已收藏过该帖子');
                                }
                                model('DocumentUserFavorites')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                Db::name('Wdocument')->where('id',$doc['doc_id'])->setInc('favorites',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('favorites',1);
                                $message = '收藏成功';
                                break;
                            case 'wf':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wf')->find();
                                $favorites = Db::name('DocumentUserFavorites')->where('user_id',$this->auth->id)->where('document_id',$doc['id'])->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已收藏过该帖子');
                                }
                                model('DocumentUserFavorites')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                Db::name('Wfdocument')->where('id',$doc['doc_id'])->setInc('favorites',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('favorites',1);
                                $message = '收藏成功';
                                break;
                            case 'wp':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wp')->find();
                                $favorites = Db::name('DocumentUserFavorites')->where('user_id',$this->auth->id)->where('document_id',$doc['id'])->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已收藏过该帖子');
                                }
                                model('DocumentUserFavorites')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                Db::name('Wpdocument')->where('id',$doc['doc_id'])->setInc('favorites',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('favorites',1);
                                $message = '收藏成功';
                                break;
                            case 'wr':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wr')->find();
                                $favorites = Db::name('DocumentUserFavorites')->where('user_id',$this->auth->id)->where('document_id',$doc['id'])->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已收藏过该帖子');
                                }
                                model('DocumentUserFavorites')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                Db::name('Resource')->where('id',$doc['doc_id'])->setInc('favorites',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('favorites',1);
                                $message = '收藏成功';
                                break;
                            case 'week':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','week')->find();
                                $favorites = Db::name('DocumentUserFavorites')->where('user_id',$this->auth->id)->where('document_id',$doc['id'])->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已收藏过该帖子');
                                }
                                model('DocumentUserFavorites')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('favorites',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('favorites',1);
                                $message = '收藏成功';# code...
                                break;
                            case 'day':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','day')->find();
                                $favorites = Db::name('DocumentUserFavorites')->where('user_id',$this->auth->id)->where('document_id',$doc['id'])->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已收藏过该帖子');
                                }
                                model('DocumentUserFavorites')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('favorites',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('favorites',1);
                                $message = '收藏成功';# code...
                                break;
                            case 'fa':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','fa')->find();
                                $favorites = Db::name('DocumentUserFavorites')->where('user_id',$this->auth->id)->where('document_id',$doc['id'])->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已收藏过该帖子');
                                }
                                model('DocumentUserFavorites')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                Db::name('Wpdocument')->where('id',$doc['doc_id'])->setInc('favorites',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('favorites',1);
                                $message = '收藏成功';# code...
                                break;        
                        }
                        break;
                    case 'fo':
                        switch ($type) {
                            case 'document':
                                $favorites = Db::name('DocumentUserFollow')->where('user_id',$this->auth->id)->where('document_id',$id)->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已点赞过该帖子');
                                }
                                $doc = Db::name('Document')->where('id',$id)->find();
                                model('DocumentUserFollow')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                switch ($doc['type']) {
                                    case 'wd':
                                        Db::name('Wdocument')->where('id',$doc['doc_id'])->setInc('likes',1);
                                        Db::name('Document')->where('id',$id)->setInc('likes',1);
                                        $message = '点赞成功';
                                        break;
                                    case 'wf':
                                        Db::name('Wfdocument')->where('id',$doc['doc_id'])->setInc('likes',1);
                                        Db::name('Document')->where('id',$id)->setInc('likes',1);
                                        $message = '点赞成功';
                                        break;
                                    case 'wp':
                                        Db::name('Wpdocument')->where('id',$doc['doc_id'])->setInc('likes',1);
                                        Db::name('Document')->where('id',$id)->setInc('likes',1);
                                        $message = '点赞成功';
                                        break;
                                    case 'wr':
                                        Db::name('Resource')->where('id',$doc['doc_id'])->setInc('likes',1);
                                        Db::name('Document')->where('id',$id)->setInc('likes',1);
                                        $message = '点赞成功';
                                        break;
                                    case 'week':
                                        Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('likes',1);
                                        Db::name('Document')->where('id',$id)->setInc('likes',1);
                                        $message = '点赞成功';# code...
                                        break;
                                    case 'day':
                                        Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('likes',1);
                                        Db::name('Document')->where('id',$id)->setInc('likes',1);
                                        $message = '点赞成功';# code...
                                        break;
                                    case 'fa':
                                        Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('likes',1);
                                        Db::name('Document')->where('id',$id)->setInc('likes',1);
                                        $message = '点赞成功';# code...
                                        break;
                                }
                                break;
                            case 'wd':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wd')->find();
                                $favorites = Db::name('DocumentUserFollow')->where('user_id',$this->auth->id)->where('document_id',$doc['id'])->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已点赞过该帖子');
                                }
                                model('DocumentUserFollow')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                Db::name('Wdocument')->where('id',$doc['doc_id'])->setInc('likes',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('likes',1);
                                $message = '点赞成功';
                                break;
                            case 'wf':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wf')->find();
                                $favorites = Db::name('DocumentUserFollow')->where('user_id',$this->auth->id)->where('document_id',$doc['id'])->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已点赞过该帖子');
                                }
                                model('DocumentUserFollow')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                Db::name('Wfdocument')->where('id',$doc['doc_id'])->setInc('likes',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('likes',1);
                                $message = '点赞成功';
                                break;
                            case 'wp':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wp')->find();
                                $favorites = Db::name('DocumentUserFollow')->where('user_id',$this->auth->id)->where('document_id',$doc['id'])->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已点赞过该帖子');
                                }
                                model('DocumentUserFollow')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                Db::name('Wpdocument')->where('id',$doc['doc_id'])->setInc('likes',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('likes',1);
                                $message = '点赞成功';
                                break;
                            case 'wr':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wr')->find();
                                $favorites = Db::name('DocumentUserFollow')->where('user_id',$this->auth->id)->where('document_id',$doc['id'])->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已点赞过该帖子');
                                }
                                model('DocumentUserFollow')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                Db::name('Resource')->where('id',$doc['doc_id'])->setInc('likes',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('likes',1);
                                $message = '点赞成功';
                                break;
                            case 'week':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','week')->find();
                                $favorites = Db::name('DocumentUserFollow')->where('user_id',$this->auth->id)->where('document_id',$doc['id'])->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已点赞过该帖子');
                                }
                                model('DocumentUserFollow')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('likes',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('likes',1);
                                $message = '点赞成功';# code...
                                break;
                            case 'day':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','day')->find();
                                $favorites = Db::name('DocumentUserFollow')->where('user_id',$this->auth->id)->where('document_id',$doc['id'])->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已点赞过该帖子');
                                }
                                model('DocumentUserFollow')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('likes',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('likes',1);
                                $message = '点赞成功';# code...
                                break;
                            case 'fa':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','fa')->find();
                                $favorites = Db::name('DocumentUserFollow')->where('user_id',$this->auth->id)->where('document_id',$doc['id'])->find();
                                if(!empty($favorites)){
                                    throw new Exception('您已点赞过该帖子');
                                }
                                model('DocumentUserFollow')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                ],true);
                                Db::name('Wpdocument')->where('id',$doc['doc_id'])->setInc('likes',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('likes',1);
                                $message = '点赞成功';# code...
                                break;        
                        }
                        break;
                    case 'comment':
                        $content = input('content');
                        if(empty($content)){
                            throw new Exception('请输入评论内容');
                        }
                        switch ($type) {
                            case 'document':
                                $doc = Db::name('Document')->where('id',$id)->find();
                                model('DocumentUserComments')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'content'=>$content
                                ],true);
                                switch ($doc['type']) {
                                    case 'wd':
                                        Db::name('Wdocument')->where('id',$doc['doc_id'])->setInc('comments',1);
                                        Db::name('Document')->where('id',$id)->setInc('comments',1);
                                        $message = '评论成功';
                                        break;
                                    case 'wf':
                                        Db::name('Wfdocument')->where('id',$doc['doc_id'])->setInc('comments',1);
                                        Db::name('Document')->where('id',$id)->setInc('comments',1);
                                        $message = '评论成功';
                                        break;
                                    case 'wp':
                                        Db::name('Wpdocument')->where('id',$doc['doc_id'])->setInc('comments',1);
                                        Db::name('Document')->where('id',$id)->setInc('comments',1);
                                        $message = '评论成功';
                                        break;
                                    case 'wr':
                                        Db::name('Resource')->where('id',$doc['doc_id'])->setInc('comments',1);
                                        Db::name('Document')->where('id',$id)->setInc('comments',1);
                                        $message = '评论成功';
                                        break;
                                    case 'week':
                                        Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('comments',1);
                                        Db::name('Document')->where('id',$id)->setInc('comments',1);
                                        $message = '评论成功';# code...
                                        break;
                                    case 'day':
                                        Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('comments',1);
                                        Db::name('Document')->where('id',$id)->setInc('comments',1);
                                        $message = '评论成功';# code...
                                        break;
                                    case 'fa':
                                        Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('comments',1);
                                        Db::name('Document')->where('id',$id)->setInc('comments',1);
                                        $message = '评论成功';# code...
                                        break;
                                }
                                break;
                            case 'wd':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wd')->find();
                                model('DocumentUserComments')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'content'=>$content
                                ],true);
                                Db::name('Wdocument')->where('id',$doc['doc_id'])->setInc('comments',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('comments',1);
                                $message = '评论成功';
                                break;
                            case 'wf':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wf')->find();
                                model('DocumentUserComments')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'content'=>$content
                                ],true);
                                Db::name('Wfdocument')->where('id',$doc['doc_id'])->setInc('comments',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('comments',1);
                                $message = '评论成功';
                                break;
                            case 'wp':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wp')->find();
                                
                                model('DocumentUserComments')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'content'=>$content
                                ],true);
                                Db::name('Wpdocument')->where('id',$doc['doc_id'])->setInc('comments',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('comments',1);
                                $message = '评论成功';
                                break;
                            case 'wr':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wr')->find();
                                model('DocumentUserComments')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'content'=>$content
                                ],true);
                                Db::name('Resource')->where('id',$doc['doc_id'])->setInc('comments',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('comments',1);
                                $message = '评论成功';
                                break;
                            case 'week':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','week')->find();
                                
                                model('DocumentUserComments')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'content'=>$content
                                ],true);
                                Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('likes',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('likes',1);
                                $message = '评论成功';# code...
                                break;
                            case 'day':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','day')->find();
                                
                                model('DocumentUserComments')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'content'=>$content
                                ],true);
                                Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('comments',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('comments',1);
                                $message = '评论成功';# code...
                                break;
                            case 'fa':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','fa')->find();
                               
                                model('DocumentUserComments')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'content'=>$content
                                ],true);
                                Db::name('Wpdocument')->where('id',$doc['doc_id'])->setInc('comments',1);
                                Db::name('Document')->where('id',$doc['id'])->setInc('comments',1);
                                $message = '评论成功';# code...
                                break;        
                        }
                        break;
                    case 'flag':
                        $nums = input('nums');
                        if(empty($nums)){
                            throw new Exception('请选择旗帜');
                        }
                        switch ($type) {
                            case 'document':
                                
                                $doc = Db::name('Document')->where('id',$id)->find();
                                model('DocumentUserFlag')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'nums'=>$nums
                                ],true);
                                switch ($doc['type']) {
                                    case 'wd':
                                        Db::name('Wdocument')->where('id',$doc['doc_id'])->setInc('flag',$nums);
                                        Db::name('Document')->where('id',$id)->setInc('flag',$nums);
                                        $message = '赠送成功';
                                        break;
                                    case 'wf':
                                        Db::name('Wfdocument')->where('id',$doc['doc_id'])->setInc('flag',$nums);
                                        Db::name('Document')->where('id',$id)->setInc('flag',$nums);
                                        $message = '赠送成功';
                                        break;
                                    case 'wp':
                                        Db::name('Wpdocument')->where('id',$doc['doc_id'])->setInc('flag',$nums);
                                        Db::name('Document')->where('id',$id)->setInc('flag',$nums);
                                        $message = '赠送成功';
                                        break;
                                    case 'wr':
                                        Db::name('Resource')->where('id',$doc['doc_id'])->setInc('flag',$nums);
                                        Db::name('Document')->where('id',$id)->setInc('flag',$nums);
                                        $message = '赠送成功';
                                        break;
                                    case 'week':
                                        Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('flag',$nums);
                                        Db::name('Document')->where('id',$id)->setInc('flag',$nums);
                                        $message = '赠送成功';# code...
                                        break;
                                    case 'day':
                                        Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('flag',$nums);
                                        Db::name('Document')->where('id',$id)->setInc('flag',$nums);
                                        $message = '赠送成功';# code...
                                        break;
                                    case 'fa':
                                        Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('flag',$nums);
                                        Db::name('Document')->where('id',$id)->setInc('flag',$nums);
                                        $message = '赠送成功';# code...
                                        break;
                                }
                                break;
                            case 'wd':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wd')->find();

                                model('DocumentUserFlag')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'nums'=>$nums
                                ],true);
                                Db::name('Wdocument')->where('id',$doc['doc_id'])->setInc('flag',$nums);
                                Db::name('Document')->where('id',$doc['id'])->setInc('flag',$nums);
                                $message = '赠送成功';
                                break;
                            case 'wf':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wf')->find();
                                model('DocumentUserFlag')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'nums'=>$nums
                                ],true);
                                Db::name('Wfdocument')->where('id',$doc['doc_id'])->setInc('flag',$nums);
                                Db::name('Document')->where('id',$doc['id'])->setInc('flag',$nums);
                                $message = '赠送成功';
                                break;
                            case 'wp':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wp')->find();
                                
                                model('DocumentUserFlag')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'nums'=>$nums
                                ],true);
                                Db::name('Wpdocument')->where('id',$doc['doc_id'])->setInc('flag',$nums);
                                Db::name('Document')->where('id',$doc['id'])->setInc('flag',$nums);
                                $message = '赠送成功';
                                break;
                            case 'wr':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','wr')->find();
                                
                                model('DocumentUserFlag')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'nums'=>$nums
                                ],true);
                                Db::name('Resource')->where('id',$doc['doc_id'])->setInc('flag',$nums);
                                Db::name('Document')->where('id',$doc['id'])->setInc('flag',$nums);
                                $message = '点赞成功';
                                break;
                            case 'week':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','week')->find();
                                
                                model('DocumentUserFlag')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'nums'=>$nums
                                ],true);
                                Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('flag',$nums);
                                Db::name('Document')->where('id',$doc['id'])->setInc('flag',$nums);
                                $message = '赠送成功';# code...
                                break;
                            case 'day':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','day')->find();
                               
                                model('DocumentUserFlag')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'nums'=>$nums
                                ],true);
                                Db::name('Wqdocument')->where('id',$doc['doc_id'])->setInc('flag',$nums);
                                Db::name('Document')->where('id',$doc['id'])->setInc('flag',$nums);
                                $message = '赠送成功';# code...
                                break;
                            case 'fa':
                                $doc = Db::name('Document')->where('doc_id',$id)->where('type','fa')->find();
                                
                                model('DocumentUserFlag')->create([
                                    'user_id'=>$this->auth->id,
                                    'document_id'=>$doc['id'],
                                    'doc_id'=>$doc['doc_id'],
                                    'type'=>$doc['type'],
                                    'nums'=>$nums
                                ],true);
                                Db::name('Wpdocument')->where('id',$doc['doc_id'])->setInc('flag',$nums);
                                Db::name('Document')->where('id',$doc['id'])->setInc('flag',$nums);
                                $message = '点赞成功';# code...
                                break;        
                        }
                        break;
                }
                Db::commit();
                $this->success($message);
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
        }
        $this->assign('t',$type);
        $this->assign('c',$ctl);
        $this->assign('id',$id);
        if($ctl == 'flag'){
           $nums = Db::name('DocumentUserFlag')->where('user_id',$this->auth->id)->where('createtime','>',strtotime(date('Y-m-d')))->sum('nums');
          $limit_flag = 5-(int)$nums;
          $limit = $limit_flag >2?3:$limit_flag;
          $this->assign('limit_flag',$limit);
          $this->assign('nums',$limit_flag);

        }
        return $this->view->fetch();
    }
    //取消收藏
    public function esc(){

    }
    //会员中心
    public function index(){
        if($this->request->isAjax()){
            $page = $this->request->post('page',1);
            $size = $this->request->post('size',10);
            $index = $this->request->post('index','');
            $sort = $this->request->post('sort','');
            $user_id = $this->request->post('user_id','');
            $where = [];
            $orderBy = [];
            $where['status'] = 'normal';
            $where['is_audit'] = 1;
            $where['user_id'] = $user_id;
           if($index){
                switch ($index) {
                    case 'hot':
                        $orderBy['once_clicks'] = 'desc';
                        break;
                    case 'featured':
                        $where['is_featured'] = 1;
                        break;
                }
            }
            if($sort){
                switch ($sort) {
                    case 'latest':
                        $orderBy['createtime'] = 'desc';
                        break;
                    case 'likes':
                        $orderBy['likes'] = 'desc';
                        break;
                    case 'comments':
                        $orderBy['comments'] = 'desc';
                        break;
                    case 'flags':
                        $orderBy['flags'] = 'desc';
                        break;
                    case 'latest_flags':
                        $orderBy['new_flag'] = 'desc';
                        break;
                    default:
                        $orderBy['createtime'] = 'desc';
                        break;
                }
            }
            $weigh = '';
            foreach ($orderBy as $key => $value) {
                $weigh .= $key.' '.$value.',';
            }
            $weigh = rtrim($weigh,',');
            $list = Db::name('Document')->where($where)->order($weigh)->page($page,$size)->select();
            foreach ($list as $key => &$value) {
                $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                $value['content'] = mb_substr(strip_tags($value['content']),0,200).'...';
                if($value['type'] == 'wd'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] = Db::name('Wdcate')->where('id',$value['document']['wdcate_id'])->find();
                    $value['document']['tags'] = Db::name('WdocumentCategory')->alias('dc')->join('wd_wdcate c', 'dc.wdcate_id = c.id')
                    ->where('dc.wdocument_id', $value['doc_id'])->field('c.name,c.id,c.image')->select();
                    
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wd')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wd')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wf'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wfdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = Db::name('WfdocumentCategory')->alias('dc')->join('wd_wfcate c', 'dc.wfcate_id = c.id')
                    ->where('dc.wfdocument_id', $value['doc_id'])->field('c.name,c.id,c.image')->select();
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wf')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wf')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wp'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wpdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wp')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wp')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wr'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wr')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wr')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'week'){
                    $value['user'] = ['nickname'=>'闻钱官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','week')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','week')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'day'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','day')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','day')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'fa'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','fa')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','fa')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                
            }
            return json(['code'=>200,'data'=>$list,'msg'=>'success']);
        
        }
        $id = $this->request->param('id');
        
        $user = Db::name('user')->where('id',$id)->find();
        $group = Db::name('UserGroup')->where('id','in',$user['group_id'])->select();
        $this->assign('group',$group);
        $this->assign('user',$user);
        $this->assign('a',Db::name('Document')->where('status','normal')->where('is_audit',1)->where('user_id',$id)->count());
        $this->assign('p',Db::name('DocumentUserComments')->where('user_id',$id)->count());
        $this->assign('l',Db::name('Document')->where('status','normal')->where('is_ling',1)->where('user_id',$id)->count());
        $this->assign('wqurl',Db::name('category')->where('id',21)->value('url'));
        $this->assign('id',$id);
        return $this->view->fetch();
    }
    //会员收藏
    public function favorites(){
        if($this->request->isAjax()){
            $page = $this->request->post('page',1);
            $size = $this->request->post('size',10);
            $user_id = $this->request->post('user_id','');

            $list = Db::name('DocumentUserFavorites')->alias('f')->join('wd_document d','d.id=f.doc_id')->where('f.user_id',$user_id)->order('d.id desc')->field('d.*')->page($page,$size)->select();
            foreach ($list as $key => &$value) {
                $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                $value['content'] = mb_substr(strip_tags($value['content']),0,200).'...';
                if($value['type'] == 'wd'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] = Db::name('Wdcate')->where('id',$value['document']['wdcate_id'])->find();
                    $value['document']['tags'] = Db::name('WdocumentCategory')->alias('dc')->join('wd_wdcate c', 'dc.wdcate_id = c.id')
                    ->where('dc.wdocument_id', $value['doc_id'])->field('c.name,c.id,c.image')->select();
                    
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wd')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wd')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wf'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wfdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = Db::name('WfdocumentCategory')->alias('dc')->join('wd_wfcate c', 'dc.wfcate_id = c.id')
                    ->where('dc.wfdocument_id', $value['doc_id'])->field('c.name,c.id,c.image')->select();
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wf')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wf')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wp'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wpdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wp')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wp')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wr'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wr')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wr')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'week'){
                    $value['user'] = ['nickname'=>'闻钱官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','week')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','week')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'day'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','day')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','day')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'fa'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','fa')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','fa')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                
            }
            return json(['code'=>200,'data'=>$list,'msg'=>'success']);
        
        }
        $id = $this->request->param('id');
        $user = Db::name('user')->where('id',$id)->find();
        $group = Db::name('UserGroup')->where('id','in',$user['group_id'])->select();
        $this->assign('group',$group);
        $this->assign('user',$user);
        $this->assign('a',Db::name('Document')->where('status','normal')->where('is_audit',1)->where('user_id',$id)->count());
        $this->assign('p',Db::name('DocumentUserComments')->where('user_id',$id)->count());
        $this->assign('l',Db::name('Document')->where('status','normal')->where('is_ling',1)->where('user_id',$id)->count());
        $this->assign('wqurl',Db::name('category')->where('id',21)->value('url'));
        $this->assign('id',$id);
        return $this->view->fetch();
    }
    public function comments(){
        if($this->request->isAjax()){
            $page = $this->request->post('page',1);
            $size = $this->request->post('size',10);
            $user_id = $this->request->post('user_id','');
            $list = Db::name('DocumentUserComments')->alias('d')->join('wd_document a','a.id=d.doc_id')->where('d.user_id',$user_id)->order('d.id desc')->field('a.*,d.content as contents,d.createtime,d.user_id')->page($page,$size)->select();
            foreach ($list as $key => &$value) {
                $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                $value['content'] = mb_substr(strip_tags($value['content']),0,200).'...';
                $user = Db::name('User')->where('id',$value['user_id'])->find();
                $value['user'] = $user;
            }
            return json(['code'=>200,'data'=>$list,'msg'=>'success']);
        }
        $id = $this->request->param('id');
        $user = Db::name('user')->where('id',$id)->find();
        $group = Db::name('UserGroup')->where('id','in',$user['group_id'])->select();
        $this->assign('group',$group);
        $this->assign('user',$user);
        $this->assign('a',Db::name('Document')->where('status','normal')->where('is_audit',1)->where('user_id',$id)->count());
        $this->assign('p',Db::name('DocumentUserComments')->where('user_id',$id)->count());
        $this->assign('l',Db::name('Document')->where('status','normal')->where('is_ling',1)->where('user_id',$id)->count());
        $this->assign('wqurl',Db::name('category')->where('id',21)->value('url'));
        $this->assign('id',$id);
        return $this->view->fetch();
    }
    //会员实战
    public function adviser(){
        $id = $this->request->param('id');
        $user = Db::name('user')->where('id',$id)->find();
        $group = Db::name('UserGroup')->where('id','in',$user['group_id'])->select();
        $this->assign('group',$group);
        $this->assign('user',$user);
        $this->assign('a',Db::name('Document')->where('status','normal')->where('is_audit',1)->where('user_id',$id)->count());
        $this->assign('p',Db::name('DocumentUserComments')->where('user_id',$id)->count());
        $this->assign('l',Db::name('Document')->where('status','normal')->where('is_ling',1)->where('user_id',$id)->count());
        $this->assign('wqurl',Db::name('category')->where('id',21)->value('url'));
        $this->assign('id',$id);
        return $this->view->fetch();
    }
}
